package com.cskaoyan.service;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.MarketBrandVo;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author yi
 * @date 2022/10/13 21:35
 */
@Service
public class AdminBrandServiceImpl implements AdminBrandService {
    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Override
    public CommonData<MarketBrand> queryByIdAndName(Integer id, String name, BasePageInfo basePageInfo) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        // marketBrandExample.setOrderByClause(basePageInfo.getSort());
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(marketBrands);
        return CommonData.data(marketBrandPageInfo);
    }

    @Override
    public MarketBrandVo createBrand(MarketBrand brand) {
        // brand.setAddTime();
        Date date = new Date();
        brand.setAddTime(date);
        brand.setUpdateTime(date);
        marketBrandMapper.insertSelective(brand);
        MarketBrandVo marketBrandVo = marketBrandToVo(brand);
        return marketBrandVo;
    }

    @Override
    public void deleteBrand(Integer id) {
        MarketBrand brand = new MarketBrand();
        brand.setId(id);
        brand.setDeleted(true);
        marketBrandMapper.updateByPrimaryKeySelective(brand);
        return;
    }

    @Override
    public MarketBrand updateBrand(MarketBrand brand) {
        brand.setUpdateTime(new Date());
        marketBrandMapper.updateByPrimaryKey(brand);
        return brand;
    }

    private MarketBrandVo marketBrandToVo(MarketBrand marketBrand) {
        MarketBrandVo brandVo = new MarketBrandVo();
        brandVo.setId(marketBrand.getId());
        brandVo.setName(marketBrand.getName());
        brandVo.setDesc(marketBrand.getDesc());
        brandVo.setPicUrl(marketBrand.getPicUrl());
        brandVo.setFloorPrice(marketBrand.getFloorPrice());
        brandVo.setAddTime(marketBrand.getAddTime());
        brandVo.setUpdateTime(marketBrand.getUpdateTime());
        return brandVo;
    }
}
